package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.SharedObject;
	
	/**
	 * ...
	 * @author patsup
	 * 
	 * 		The Main class basically handles switching between "menus".
	 * 	DisplayNewRound() and ContinueNewRound() are similar except one is coming from the StartMenu,
	 * 	and the other is coming from the PGame.  PlayRound() "displays" the PGame, as well as resets a round.
	 * 	ResetGame() will reset an entire game/session/whatev (e.g. from StartMenu's New Game or Load Game).
	 * 
	 * *** public functions
	 * 
	 * ResetGame():void
	 * ReturnToStartMenu(previousMenu:Menu):void
	 * DisplayNewRound(previousMenu:Menu):void
	 * ContinueNewRound():void
	 * PlayRound(previousMenu:Menu):void
	 * DisplayAchievMenu(previousMenu:Menu):void
	 * DisplayLoadMenu(previousMenu:Menu):void
	 * DisplayInfoMenu(previousMenu:Menu):void
	 * DisplayHighScores(previousMenu:Menu):void
	 * 
	 */
	public class Main extends Sprite 
	{
		public const DEBUG_MODE:Boolean = true;				//set to false to hide session stats
		private var startMenu:StartMenu;
		
		private var loadMenu:LoadMenu;
		private var infoMenu:InfoMenu;
		private var highScoreMenu:HSMenu;
		
		private var pirateGame:PGame;
		private var preRoundMenu:PreRoundMenu;
		private var achievMenu:AchievMenu;
		
		private var savedData:SharedObject;
		
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			//savedData = SharedObject.getLocal("PirateGameSaveData");
			
			startMenu = new StartMenu(this);
			addChild(startMenu);
			
			pirateGame = new PGame(this);
			preRoundMenu = new PreRoundMenu(this, pirateGame);
			achievMenu = new AchievMenu(this);
			
			highScoreMenu = new HSMenu(this);
			loadMenu = new LoadMenu(this);
			infoMenu = new InfoMenu(this);
			
		}
		
		//DeleteGame()
		//Deletes the data for the selected game
		public function DeleteGame(gameNumber:int):void
		{
			SharedObject.getLocal("PirateGameSaveData" + gameNumber).clear();
			loadMenu.Initialize();												//refresh it
		}
		//LoadGame()
		//Loads the game. 
		public function LoadGame(previousMenu:Menu, savedData:SharedObject):void
		{
			this.savedData = savedData;						//record the SharedObject so we know what to write to during the game
			if (savedData.data.hasData == true) {
				pirateGame.InitLoadGame(savedData.data);	//load that game
			} else {
				pirateGame.InitNewGame();					//start a new game
			}
			DisplayNewRound(previousMenu);
		}
		
		//SaveGame()
		//Save the Game's session stats
		public function SaveGame():void
		{
			trace("Saving Game Data");
			pirateGame.GetSS().SaveGameData(savedData);
		}
		//ResetGame()
		//Reset the Game to New state
		// --- deprecated... don't need to let anyone else reset/start new game, because
		// --- now we just use LoadGame() to determine if there should be a reset/new game
		public function ResetGame():void
		{
			pirateGame.InitNewGame();
		}
		//ReturnToMain()
		//return to display start menu
		public function ReturnToStartMenu(previousMenu:Menu):void
		{
			removeChild(previousMenu);
			addChild(startMenu);
		}
		
		//DisplayNewRound()
		//display the pre-round menu before pirate game
		public function DisplayNewRound(previousMenu:Menu):void
		{
			removeChild(previousMenu);
			addChild(preRoundMenu);
		}
		//ContinueNewRound()
		//display the pre-round menu before continuing pirate game, after completing a round already
		public function ContinueNewRound():void
		{
			removeChild(pirateGame);
			addChild(preRoundMenu);
			
			if(DEBUG_MODE) addChild(pirateGame.GetSS());	//temporarily, so u can see stats
		}
		
		//PlayRound()
		//display the pirate game ("actually" start playing)
		public function PlayRound(previousMenu:Menu):void
		{
			removeChild(previousMenu);
			pirateGame.ResetRound();
			addChild(pirateGame);
		}
		
		//DisplayAchievMenu()
		//display the achievements menu
		public function DisplayAchievMenu(previousMenu:Menu):void
		{
			removeChild(previousMenu);
			addChild(achievMenu);
		}
		
		//DisplayLoadMenu()
		//display the load menu
		public function DisplayLoadMenu(previousMenu:Menu):void
		{
			removeChild(previousMenu);
			loadMenu.Initialize();		//refresh it before we display it cuz it coulda changed
			addChild(loadMenu);
		}
		
		//DisplayInfoMenu()
		//display the info menu
		public function DisplayInfoMenu(previousMenu:Menu):void
		{
			removeChild(previousMenu);
			addChild(infoMenu);
		}
		
		//DisplayHighScores()
		//display the high scores
		public function DisplayHighScores(previousMenu:Menu):void
		{
			removeChild(previousMenu);
			addChild(highScoreMenu);
		}
	}
	
}